var app = getApp();
var api = require('../../api.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var page = this;
    var rdsession = wx.getStorageSync('rdsession');
    if (!rdsession) {
      //暂存参数
      if (options) {
        wx.setStorageSync("tmp_options", options)
      }
      wx.navigateTo({
        url: '/pages/usersq/usersq',
      })
    }
    if (options){
      var id = options.id;
      if (id){
        app.request({
          url: api.teacher.detail,
          method:'POST',
          data: {
            id:id
          },
          success: function (res) {
            if (res.errcode == 0) {
              var content = res.data.content +'<span> </span>'
              WxParse.wxParse("detail", "html", content, page, 5);
              page.setData({
                list: res.data
              });
              wx.setNavigationBarTitle({
                title: res.data.name,
              })
            }else{
              wx.showModal({
                title: '提示',
                content: '讲师不存在',
                showCancel: false,
                confirmText: '去讲师列表',
                success: function (res) {
                  if (res.confirm) {
                    wx.redirectTo({
                      url: '/pages/teacher-info/teacher-info',
                    })
                  }
                }
              })
            }
          }
        });
      }

     //锁定上级
      // if (options.uid) {
      //   var uid = options.uid;
      //   console.log(uid);
      //   if (uid) {
      //     page.agent(uid);
      //   }
      // }
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var list = this.data.list;
    var user_info = wx.getStorageSync('user_info');
    var result = {
      title: list.name,
      path: "/pages/teacher-detail/teacher-detail?id=" + list.id + "&pid=" + user_info.user_id,
      imageUrl: list.icon,
      success: function (res) {
      },
      fail: function (res) {
        // 转发失败
      }
    };
    return result;
  },

  agent: function (uid) {
    app.request({
      url: api.distribution.agent,
      data: {
        puid: uid
      },
      success: function (res) {
        if (res.code == 0) {

        }
      },
      complete: function () {

      }

    });
  },
})